package com.central.apps.handler.purchase;

import com.central.app.common.base.BaseHandler;
import com.central.app.service.HandlerService;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 处理到货验收单业务逻辑
 *
 * @author lidachao
 * @date 2022/8/11
 */
@Component
@Slf4j
public class ArrivalAcceptanceHandler extends BaseHandler {

    @Autowired
    private HandlerService handlerService;

    @Override
    public void beforDelete(String app, String objName, Long id, boolean isLogic) {
        if (objName.equals("gq_arrivalacceptanceline")) {
            // 查询采购订单明细
            List<Map<String, Object>> aalinelist = handlerService.findBySql("select arrivnum, totalcost from gq_arrivalacceptanceline where id = " + id);
            Map<String, Object> aalineMap = aalinelist.get(0);

            String arrivnum = (String) aalineMap.get("arrivnum");
            float totalcost = (float) aalineMap.get("totalcost");

            // 查询采购订单信息
            List<Map<String, Object>> aalist = handlerService.findBySql("select totalcost from gq_arrivalacceptance where arrivnum = '" + arrivnum + "'");
            Map<String, Object> aaMap = aalist.get(0);
            float aaTotalcost = (float) aaMap.get("totalcost");

            handlerService.updateBySql("update gq_arrivalacceptance set totalcost = " + (aaTotalcost-totalcost) + " where arrivnum = '" + arrivnum + "'");
        }
    }

    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        String arrivnum = MapUtils.getString(data, "arrivnum");
        List<Map<String, Object>> resultList = handlerService.findBySql("select totalcost from gq_arrivalacceptanceline where arrivnum = '" + arrivnum + "'");

        float aaTtotalcost = 0f;
        if (CollectionUtils.isNotEmpty(resultList)) {
            for (Map<String, Object> map : resultList) {
                float totalcost = (float) map.get("totalcost");

                aaTtotalcost = aaTtotalcost + totalcost;
            }

            handlerService.updateBySql("update gq_arrivalacceptance set totalcost = " + aaTtotalcost + " where arrivnum = '" + arrivnum + "'");
        }
    }

    @Override
    public void afterExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op, String event) throws Exception {
        if(flowNode.getId().equals("approval")){
            // 查询到货验收单信息
            String woSql = "select arrivnum from gq_arrivalacceptance where id = " + id;
            List<Map<String, Object>> woList = handlerService.findBySql(woSql);

            if (CollectionUtils.isNotEmpty(woList)) {
                Map woMap = woList.get(0);
                String arrivnum = (String) woMap.get("arrivnum");

                StringBuilder searchSql = new StringBuilder();
                searchSql.append("select poid, ponum, polineid, polinenum, contractid, contractnum, " +
                        "contractlineid, contractlinenum, orderqty, linecost, totalcost from gq_arrivalacceptanceline where arrivnum = '" + arrivnum + "'");

                List<Map<String, Object>> resultList = handlerService.findBySql(searchSql.toString());
                if (CollectionUtils.isNotEmpty(resultList)) {
                    for (Map<String, Object> map : resultList) {
                        String ponum = (String) map.get("ponum");
                        String contractnum = (String) map.get("contractnum");
                        int orderqty = (int) map.get("orderqty");

                        StringBuilder updateSql = new StringBuilder();
                        // 计算采购订单未收货数量
                        if (StringUtils.isNotEmpty(ponum)) {
                            int polineid = (int) map.get("polineid");
                            List<Map<String, Object>> tempList = handlerService.findBySql("select orderqty, receivedlinecost, receivedqty, receivedunitcost, " +
                                    "receivedtotalcost, receiptscomplete, inspectreceive, inspectqty from gq_poline where id = " + polineid);

                            Map<String, Object> tempMap = tempList.get(0);

                            // 计算剩余未验收数量及其他成本
                            String receiptscomplete = (String) tempMap.get("receiptscomplete");
                            int inspectreceive = (int) tempMap.get("inspectreceive");
                            int inspectqty = (int) tempMap.get("inspectqty");

                            inspectqty = inspectqty + orderqty;
                            inspectreceive = inspectreceive - orderqty;

                            if (inspectreceive == 0) {
                                receiptscomplete = "1";
                            }
                            if (StringUtils.isEmpty(receiptscomplete)) {
                                receiptscomplete = "0";
                            }

                            updateSql.append("update gq_poline set receiptscomplete = '" + receiptscomplete
                                    + "',inspectreceive = " + inspectreceive
                                    + ",inspectqty = " + inspectqty + " where id = " + polineid);
                        }

                        // 计算合同未收货数量
                        if (StringUtils.isNotEmpty(contractnum)) {
                            int contractlineid = (int) map.get("contractlineid");
                            List<Map<String, Object>> tempList = handlerService.findBySql("select orderqty from where id = " + contractlineid);
                            // 计算剩余未收货数量
                        }

                        handlerService.updateBySql(updateSql.toString());
                    }
                }
            }
        }
    }
}
